//
//  BIDMeetingSignViewController.m
//  JuanChengOA
//
//  Created by mal on 2017/7/28.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDMeetingSignListViewController.h"
#import "BIDMeetingSignViewController.h"
#import "BIDJoinMeetingCell.h"

//参会记录
static NSString *joinMeetingListURL = @"noticeAndroid!listNoticeSign.action";

@interface BIDMeetingSignListViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    BIDJoinMeetingCell *_tempCell;
    BOOL _isRegister;
}
@end

@implementation BIDMeetingSignListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"参会记录";
    [self loadData];
    _tempCell = [[[NSBundle mainBundle] loadNibNamed:@"BIDJoinMeetingCell" owner:self options:nil] lastObject];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"签到" style:UIBarButtonItemStylePlain target:self action:@selector(toSign)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], joinMeetingListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"userId=%@&page=%ld&rows=%ld", [AppDelegate getUserId], self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSArray *arr = responseDic[@"rows"];
            [self.dataSourceArr addObjectsFromArray:arr];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
        }
        [self.myTableView reloadData];
    }];
}

- (void)toSign
{
    BIDMeetingSignViewController *vc = [[BIDMeetingSignViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_isRegister)
    {
        _isRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDJoinMeetingCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    NSDictionary *dic = self.dataSourceArr[row];
    NSDictionary *noticeDic = dic[@"notice"];
    BIDJoinMeetingCell *meetingCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    meetingCell.titleLable.text = noticeDic[@"title"];
    meetingCell.meetingTimeLabel.text = noticeDic[@"meetingTime"];
    meetingCell.meetingPlaceLabel.text = noticeDic[@"meetingPlace"];
    meetingCell.meetingOrganizerLabel.text = noticeDic[@"createUserName"];
    meetingCell.signTimeLabel.text = dic[@"signTime"];
    cell = meetingCell;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 154.f;
}

@end
